﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using Netfraction.Common;
using Netfraction.Common.DataStorage;

namespace NetfractionHubUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static XmlDataStore _settingsStore;

        public MainWindow()
        {
            InitializeComponent();
        }

        private static bool CheckStoreExists()
        {
            _settingsStore = new XmlDataStore(NetfractionHub.Settings.HubDataSource.Source, "Hub.xml");

            if (_settingsStore == null)
            {
                return false;
            }
            return true;
        }

        public static System.Net.IPAddress ResolveDnsAddress(string address)
        {
            System.Net.IPHostEntry iphe = System.Net.Dns.GetHostEntry(address);
            if (iphe.AddressList.Length > 0)
            {
                return iphe.AddressList[0];
            }

            return System.Net.IPAddress.None;
        }


        public static Netfraction.LocalMachine.Client mClient;
        static Netfraction.RemoteMachine.Hub hub;
        private const string KLORHUB_DNS = "klorhub.ath.cx";
        enum HubsEnum : int
        {
            Hubbie = 411,
            uHub = 1511,
            ADCH = 2780,
            DSHubPublicTestHub = 1111,
            Netfraction = 6356,
            ADCPortal = 16591,
            Elite = 2424
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckStoreExists())
                return;

            XPathNavigator xNav = _settingsStore.CreateNavigator(false);

            string myPid = xNav.SelectSingleNode(@"//Settings/HubIdentity/UserInterfaceClientId").Value;
            Console.WriteLine(myPid);

            mClient = Netfraction.LocalMachine.Client.GetInstance(myPid);
            Netfraction.LocalMachine.Client.OnClientIdSet += new Netfraction.LocalMachine.ClientIdEventHandler(Client_OnClientIDSet);
            hub = new Netfraction.RemoteMachine.Hub();
            
            hub.Connection.Connect(new System.Net.IPEndPoint(ResolveDnsAddress(KLORHUB_DNS), (int)HubsEnum.Netfraction));
        }

        void Client_OnClientIDSet(Netfraction.LocalMachine.ClientIdEventArgs args)
        {
            Netfraction.Identity ident = new Netfraction.Identity();
            ident.PrivateId = args.PrivateId;
            ident.ClientId = args.ClientId;

            ident.Nickname = "HubGUI";
            ident.Password = string.Empty; ;
            ident.Description = string.Empty;
            ident.Email = string.Empty;
            ident.ShareSize = 0;
            ident.SharedFilesCount = 0;
            ident.MaximumDownloadSpeed = 13107200;
            ident.MaximumUploadSpeed = 13107200;
            ident.UploadSlots = 0;
            ident.AwayStatus = Netfraction.IdentityAwayStatus.Online;

            mClient.SetIdentity(ident);
        }

        public static string GenerateClientId(System.Security.Cryptography.HashAlgorithm hashAlgorithm, string input)
        {
            if (string.IsNullOrEmpty(input) || hashAlgorithm == null)
                return string.Empty;

            return GenerateClientId(hashAlgorithm, System.Text.Encoding.UTF8.GetBytes(input));
        }

        public static string GenerateClientId(System.Security.Cryptography.HashAlgorithm hashAlgorithm, byte[] input)
        {
            if (input.Length == 0 || hashAlgorithm == null)
                return string.Empty;

            hashAlgorithm.Initialize();
            byte[] hash = hashAlgorithm.ComputeHash(input);
            return Base32.Encode(hash).Substring(0, 39);
        }
    }
}
